package com.banyechan.lingo_words.exception;

import com.banyechan.lingo_words.entity.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;



/**
 * 全局异常处理
 *
 * @author Jax
 * @date 2021/1/18 13:56
 * @company 上海佳一智慧健康管理有限公司
 */
@Slf4j
@RestControllerAdvice
public class ExceptionHandlerResolver {

    /**
     * 内部接口前缀
     */
    public static final String INNER_API_PREFIX = "/feign";

    /**
     * 全局异常处理
     *
     * @param ex the e
     * @return R
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public R<?> handleGlobalException(HttpServletRequest request, Exception ex) {
        String url = request.getRequestURI();
        if (request.getMethod().toLowerCase().equals("get") && StringUtils.isNotBlank(request.getQueryString())) {
            url += "?".concat(request.getQueryString());
        }
        log.error("全局异常信息 api=[{}],ex={}", url, ex.getMessage().isEmpty() ? "null pointer" : ex.getMessage(), ex);
        R<Object> r = R.fail(BusinessExceptionEnum.SYSTEM_ERROR.getMsg());
        if (ex instanceof HttpRequestMethodNotSupportedException) {
            BusinessExceptionEnum exceptionEnum = BusinessExceptionEnum.COMMON_METHOD_TYPR_NOT_SUPPORTED;
            r.setCode(exceptionEnum.getCode());
            r.setMessage(exceptionEnum.getMsg());
            r.setData(ex.getMessage());
        } else if (ex instanceof HttpMessageNotReadableException
                || ex instanceof HttpMediaTypeNotSupportedException
                || ex instanceof MissingServletRequestParameterException) {
            BusinessExceptionEnum exceptionEnum = BusinessExceptionEnum.COMMON_PARAM_TYPR_NOT_SUPPORTED;
            r.setCode(exceptionEnum.getCode());
            r.setMessage(exceptionEnum.getMsg());
            r.setData(ex.getMessage());
        } else if (ex instanceof DataAccessException) {
            BusinessExceptionEnum exceptionEnum = BusinessExceptionEnum.COMMON_PARAM_TYPR_NOT_SUPPORTED;
            r.setCode(exceptionEnum.getCode());
            r.setMessage(exceptionEnum.getMsg());
            r.setData(ex.getMessage());
        } else if (ex.getMessage().contains("401 Unauthorized")) {
            //feign.FeignException$Unauthorized
            BusinessExceptionEnum exceptionEnum = BusinessExceptionEnum.COMMON_TOKEN_INVALID_OR_EXPIRED;
            log.info("401认证失败, token: {}", request.getHeader(org.springframework.http.HttpHeaders.AUTHORIZATION));
            r.setCode(exceptionEnum.getCode());
            r.setMessage(exceptionEnum.getMsg());
            r.setData(ex.getMessage());
        }
        return r;
    }
}